/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.view.tags;

import java.util.StringTokenizer;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 *
 * @author Walter Luetgenau
 */
public class NavigationTag extends TagSupport {
    private String path;
    private String base;
    
    /** Creates a new instance of NavigationTag */
    public NavigationTag() {
    }
    
    public int doStartTag() throws JspException {
        try {
            if ( getPath() == null || getPath().length() == 0 || getBase() == null || getBase().length() == 0)
                return SKIP_BODY;
            
            StringBuffer currentPath = new StringBuffer();
            StringTokenizer tokenizer = new StringTokenizer(getPath(), "/");
            int level = 0;
            while( tokenizer.hasMoreTokens()) {
                String pathComponent = tokenizer.nextToken();
                
                currentPath.append("/" + pathComponent);
                if ( level == 0)
                    pageContext.getOut().print("<a href=\"" + getBase() + "\">jcr:root</a>");
                
                pageContext.getOut().print("/");
                
                pageContext.getOut().print("<a href=\"" + getBase() + currentPath.toString() + "\">" + pathComponent + "</a>");
                
                level++;
            }
            return SKIP_BODY;
        } catch( Exception e) {
            throw new JspException(e);
        }
    }
    
    public String getPath() {
        return path;
    }
    
    public void setPath(String path) {
        this.path = path;
    }
    
    public String getBase() {
        return base;
    }
    
    public void setBase(String base) {
        this.base = base;
    }
}
